<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>取消订单按钮移除测试</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .test-section { margin: 20px 0; padding: 15px; border: 1px solid #ccc; }
        .success { color: green; }
        .error { color: red; }
        .info { color: blue; }
        .test-btn { padding: 8px 12px; background: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; margin: 5px; }
        .test-btn:hover { background: #0056b3; }
        .order-preview { border: 1px solid #ddd; padding: 15px; margin: 10px 0; border-radius: 4px; }
    </style>
</head>
<body>
    <h1>取消订单按钮移除测试</h1>
    
    <div class="test-section">
        <h3>修改说明</h3>
        <div class="info">
            <strong>移除内容：</strong><br>
            • 从订单操作按钮中移除了"取消订单"按钮<br>
            • 删除了 cancelOrder 方法<br>
            • 只保留"确认收货"和"再次购买"按钮
        </div>
    </div>
    
    <div class="test-section">
        <h3>1. 模拟用户登录</h3>
        <button class="test-btn" onclick="simulateLogin()">模拟用户登录</button>
        <div id="loginResult"></div>
    </div>
    
    <div class="test-section">
        <h3>2. 测试订单按钮显示</h3>
        <button class="test-btn" onclick="testOrderButtons()">测试订单按钮</button>
        <div id="buttonTestResult"></div>
    </div>
    
    <div class="test-section">
        <h3>3. 打开订单页面验证</h3>
        <button class="test-btn" onclick="openOrdersPage()">打开订单页面</button>
        <div id="pageResult"></div>
    </div>
    
    <div class="test-section">
        <h3>4. 调试信息</h3>
        <div id="debugInfo"></div>
    </div>

    <!-- 引入必要的脚本 -->
    <script src="js/api.js"></script>
    <script src="js/auth.js"></script>
    <script src="js/orders.js"></script>
    
    <script>
        // 重写console.log来显示在页面上
        const originalLog = console.log;
        const originalWarn = console.warn;
        const originalError = console.error;
        
        function addToConsole(message, type = 'info') {
            const debugDiv = document.getElementById('debugInfo');
            const timestamp = new Date().toLocaleTimeString();
            debugDiv.innerHTML += `<div class="${type}">[${timestamp}] ${message}</div>`;
            debugDiv.scrollTop = debugDiv.scrollHeight;
        }
        
        console.log = function(...args) {
            originalLog.apply(console, args);
            addToConsole(args.join(' '), 'info');
        };
        
        console.warn = function(...args) {
            originalWarn.apply(console, args);
            addToConsole(args.join(' '), 'error');
        };
        
        console.error = function(...args) {
            originalError.apply(console, args);
            addToConsole(args.join(' '), 'error');
        };
        
        // 模拟用户登录
        function simulateLogin() {
            const resultDiv = document.getElementById('loginResult');
            try {
                const userData = {
                    username: 'Mike',
                    user_id: 'USER001',
                    email: 'mike@example.com',
                    phone: '1234567890',
                    user_type: 'Customer',
                    token: 'test-token-mike'
                };
                
                localStorage.setItem('currentUser', JSON.stringify(userData));
                localStorage.setItem('userData', JSON.stringify(userData));
                localStorage.setItem('authToken', userData.token);
                
                if (window.authManager) {
                    window.authManager.currentUser = userData;
                    window.authManager.isLoggedIn = true;
                    window.authManager.userType = userData.user_type;
                }
                
                resultDiv.innerHTML = '<div class="success">✓ 用户登录成功</div>';
                console.log('用户登录成功');
            } catch (error) {
                resultDiv.innerHTML = '<div class="error">✗ 登录失败: ' + error.message + '</div>';
                console.error('登录失败:', error);
            }
        }
        
        // 测试订单按钮显示
        function testOrderButtons() {
            const resultDiv = document.getElementById('buttonTestResult');
            try {
                console.log('开始测试订单按钮显示...');
                
                // 创建模拟订单数据
                const testOrders = [
                    {
                        order_number: 'ORD123456789',
                        status: 'Processing',
                        total_amount: 100.00,
                        created_at: '2025-10-29T10:00:00Z'
                    },
                    {
                        order_number: 'ORD987654321',
                        status: 'Shipped',
                        total_amount: 200.00,
                        created_at: '2025-10-29T11:00:00Z'
                    },
                    {
                        order_number: 'ORD555666777',
                        status: 'Delivered',
                        total_amount: 300.00,
                        created_at: '2025-10-29T12:00:00Z'
                    }
                ];
                
                let buttonTestResults = '';
                
                testOrders.forEach(order => {
                    // 模拟 getActionButtons 方法
                    const buttons = [];
                    switch (order.status) {
                        case 'Shipped':
                            buttons.push(`<button class="btn btn-primary">确认收货</button>`);
                            break;
                        case 'Delivered':
                            buttons.push(`<button class="btn btn-outline">再次购买</button>`);
                            break;
                    }
                    
                    const hasCancelButton = buttons.some(btn => btn.includes('取消订单'));
                    const buttonText = buttons.join(' ');
                    
                    buttonTestResults += `
                        <div class="order-preview">
                            <strong>订单 ${order.order_number} (${order.status})</strong><br>
                            按钮: ${buttonText || '无操作按钮'}<br>
                            包含取消按钮: ${hasCancelButton ? '是' : '否'}
                        </div>
                    `;
                });
                
                resultDiv.innerHTML = `
                    <div class="success">✓ 按钮测试完成</div>
                    ${buttonTestResults}
                `;
                
                console.log('订单按钮测试完成');
            } catch (error) {
                resultDiv.innerHTML = '<div class="error">✗ 按钮测试失败: ' + error.message + '</div>';
                console.error('按钮测试失败:', error);
            }
        }
        
        // 打开订单页面
        function openOrdersPage() {
            const resultDiv = document.getElementById('pageResult');
            try {
                console.log('打开订单页面...');
                window.open('orders.html', '_blank');
                resultDiv.innerHTML = '<div class="success">✓ 订单页面已打开，请检查是否还有"取消订单"按钮</div>';
                console.log('订单页面已打开');
            } catch (error) {
                resultDiv.innerHTML = '<div class="error">✗ 打开页面失败: ' + error.message + '</div>';
                console.error('打开页面失败:', error);
            }
        }
        
        // 页面加载完成后运行
        window.addEventListener('load', () => {
            console.log('取消订单按钮移除测试页面加载完成');
        });
    </script>
</body>
</html>
